const { mysql } = require("../../db");

// 软删除
exports.SoftDel = async (req, res, table) => {
	const { id, is_delete } = req.body;

	try {
		const sqlStr = `UPDATE ${table} SET deleter = ?,del_date = NOW(),is_delete = ? WHERE id = ?`;
		const [results] = await mysql.query(sqlStr, [req.user.id, is_delete, id]);
		if (results.affectedRows !== 1) return res.error("删除失败！");
		return res.success("删除成功！");
	} catch (err) {
		return res.error(err);
	}
};

// 是否展示
exports.IsShow = async (req, res, table) => {
	const { id, is_show } = req.body;

	try {
		const sqlStr = `UPDATE ${table} SET is_show = ? WHERE id = ?`;
		const [results] = await mysql.query(sqlStr, [is_show, id]);
		if (results.affectedRows !== 1) return res.error("切换失败！");
		res.success("切换成功！");
	} catch (err) {
		return res.error(err);
	}
};

// 真删除
exports.Delete = async (req, res, table) => {
	try {
		const sqlStr = `DELETE FROM ${table} WHERE id = ?`;
		const [results] = await mysql.query(sqlStr, [req.params.id]);
		if (results.affectedRows !== 1) return res.error("删除失败！");
		res.success("信息删除成功！");
	} catch (err) {
		return res.error(err);
	}
};
